﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Services;
using Domain.Entities;
using Domain.Repositories;
using Domain.Enums;
using Domain.UnitOfWork;

namespace Logic
{
    public class QuizService:IQuizService
    {
        IQuizRepository quizRepository;
        IQuestionRepository questionRepository;
        IQuestionInQuizRepository questionInQuizRepository;
        public QuizService(IQuizRepository quizRepository , IQuestionRepository questionRepository, IQuestionInQuizRepository questionInQuizRepository)
        {
            this.quizRepository = quizRepository;
            this.questionRepository = questionRepository;
            this.questionInQuizRepository = questionInQuizRepository;
        }
        public QuestionInQuiz FormQuestionForUser(User user, Chapter chapter)
        {
            if (chapter!=null)
            {
                var question = FindQuestionInDatabase(user, chapter);
                if (question == null)
                {
                    var newQuiz = CreateNewQuiz(user, chapter);
                    if (newQuiz.Questions.Count() > 0)
                    {
                        question = newQuiz.Questions.FirstOrDefault();
                    }
                    else
                    {
                        return null;
                    }
                }

                return question;
            }
            return null;
        }

        public bool IfThereIsUncompletedTestForUser(User user, Chapter chapter)
        {
            if (chapter == null) return false;
            var results = quizRepository.GetChapterResults(chapter, user);
            if (results!=null && results.Questions.Count(x => x.QuestionStatus == QuestionStatus.NotAnswered) > 0)
                return true;
            return false;

        }

        public void PassAnswerToOpenQuestionInQuiz(int questionInQuizId, string Answer)
        {
            var questionInQuiz = questionInQuizRepository.Get(questionInQuizId);
            var correctAnswer = questionInQuiz.Question.Answers.FirstOrDefault();
            if (correctAnswer!=null)
            {
                if (correctAnswer.AnswerText.ToLower()==Answer.ToLower())
                {
                    questionInQuiz.QuestionStatus = QuestionStatus.Right;
                }
                else
                {
                    questionInQuiz.QuestionStatus = QuestionStatus.Wrong;
                }
            }
            questionInQuizRepository.Save(questionInQuiz);
        }

        public void PassAnswerToRadioQuestionInQuiz(int questionInQuizId, int AnswerId)
        {
            var questionInQuiz = questionInQuizRepository.Get(questionInQuizId);
            var correctAnswer = questionInQuiz.Question.Answers.FirstOrDefault(x=>x.Correctness);
            if (correctAnswer != null)
            {
                if (correctAnswer.Id==AnswerId)
                {
                    questionInQuiz.QuestionStatus = QuestionStatus.Right;
                }
                else
                {
                    questionInQuiz.QuestionStatus = QuestionStatus.Wrong;
                }
            }
            questionInQuizRepository.Save(questionInQuiz);
        }

        public void PassAnswerToCheckQuestionInQuiz(int questionInQuizId, int[] AnswersIds)
        {
            var questionInQuiz = questionInQuizRepository.Get(questionInQuizId);
            var correctAnswers = questionInQuiz.Question.Answers.Where(x => x.Correctness);
            int correctAnswersCount = correctAnswers.Count(correctAnswer => AnswersIds.Contains(correctAnswer.Id));
            if (correctAnswersCount==correctAnswers.Count(x=>x.Correctness))
            {
                questionInQuiz.QuestionStatus = QuestionStatus.Right;
            }
            else
            {
                questionInQuiz.QuestionStatus = QuestionStatus.Wrong;  
            }
            questionInQuizRepository.Save(questionInQuiz);
        }

        public int GetChapterIdForQuestionInQuiz(int questionInQuizId)
        {
            if (questionInQuizRepository != null)
                return questionInQuizRepository.Get(questionInQuizId).Question.Chapter.Id;
            else
                return -1;
        }


        private QuestionInQuiz FindQuestionInDatabase(User user, Chapter chapter)
        {
            var quiz = quizRepository.GetChapterResults(chapter, user);
            if (quiz != null)
            {
                var question = quiz.Questions.FirstOrDefault(x => x.QuestionStatus == QuestionStatus.NotAnswered);
                return question;
            }
            return null;
        }

        private Quiz CreateNewQuiz(User user, Chapter chapter)
        {
                var quiz = new Quiz();
                quiz.User = user;
                quiz.Chapter = chapter;
                List<QuestionInQuiz> questionsToQuiz = new List<QuestionInQuiz>();
                foreach (var question in questionRepository.GetListOfRandomQuestions(chapter, 10))
                {
                    var questionInQuiz = new QuestionInQuiz()
                                             {Question = question, QuestionStatus = QuestionStatus.NotAnswered};
                    questionInQuizRepository.Save(questionInQuiz);
                    questionsToQuiz.Add(questionInQuiz);
                }
                quiz.Questions = questionsToQuiz;
                quizRepository.Save(quiz);
                return quiz;
        }

    }
}
